<?php

require_once("Push/push_functions.php");

function addDevice() {
	// return json response
	//$json = array();
	$file = 'log_device.txt';
	// The new person to add to the file
	$p1 = "Entra\n";
	// Write the contents to the file, 
	// using the FILE_APPEND flag to append the content to the end of the file
	// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
	file_put_contents($file, $p1, FILE_APPEND | LOCK_EX);
 	//TODO pasar los parametros a POST
 	//$nameUser  = $_GET["name"];
	//$nameEmail = $_GET["email"];

	$nameUser  = $_POST["name"];
	$nameEmail = $_POST["email"];
 
	// GCM Registration ID got from device
	$gcmRegID  = $_POST["regId"];
 	//$gcmRegID  = $_GET["regId"];
	/**
	 * Registering a user device in database
	 * Store reg id in users table
	 */
	if (isset($nameUser)
	     && isset($nameEmail)
	     && isset($gcmRegID)) {
	     
	    // Store user details in db
	    $res = storeUser($nameUser, $nameEmail, $gcmRegID);

		//DFM no se perque envia ahi	 
//	    $registatoin_ids = array($gcmRegID);
//	    $message = array("product" => "shirt");
	 
//	    $result = send_push_notification($registatoin_ids, $message);
	    echo $res;
	} else {
	    // user details not found
	}
}

function sendPush() {
 	//TODO pasar los parametros a POST
 	//TODO: Definir los tipos y las categorias
 	$tipo  = $_GET["tipo"];
 	$id  = $_GET["id"];
 	$id_cat  = $_GET["id_cat"];
 	$titulo  = $_GET["titulo"];
 
	if (isset($titulo) && isset($tipo) && isset($id)) {
		//DFM no se perque envia ahi	 
		//$registatoin_ids = array($gcmRegID);
	    $message = array("tipo" => $tipo, "id" => $id, "id_cat" => $id_cat, "titulo" => $titulo, "message" => "NEEDED");
	 
	    $result = send_push_to_all($message);
	    echo $result;
	} else {
	    // user details not found
	    echo "Parametros incorrectos";
	}
}
	
?>
